package com.tuanzi.loan.web.bean.family.sms;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.constants.sms.SmsVoiceSendTaskStatus;
import com.tuanzi.loan.business.entity.family.ProductInfo;
import com.tuanzi.loan.business.entity.family.RegisterRecommendActivity;
import com.tuanzi.loan.business.entity.sms.SmsCalculateTask;
import com.tuanzi.loan.business.entity.sms.SmsCalculateTaskStatus;
import com.tuanzi.loan.business.entity.sms.SmsUserDefinition;
import com.tuanzi.loan.business.entity.sms.SmsVoiceSendTask;
import com.tuanzi.loan.business.entity.statistics.ActivityChannelConfig;
import com.tuanzi.loan.business.entity.system.Dictionary;
import com.tuanzi.loan.business.service.family.RegisterRecommendActivityService;
import com.tuanzi.loan.business.service.sms.PhoneLocationService;
import com.tuanzi.loan.business.service.sms.SmsCalculateTaskService;
import com.tuanzi.loan.business.service.sms.SmsUserDefinitionService;
import com.tuanzi.loan.business.service.sms.SmsVoiceSendTaskService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.util.GsonUtils;
import com.tuanzi.loan.vo.request.family.SmsCondition;
import com.tuanzi.loan.web.configuration.JSFBean;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;
import java.util.Map;

import static com.tuanzi.loan.business.constants.DictionaryCode.*;

/**
 * @Author: chenweijun
 * @Date: 2018/10/23 13:46
 * @Description:
 */
@Data
@Slf4j
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class SmsVoiceConditionViewBean extends JSFBean {

    private SmsCondition smsCondition;
    private SmsCalculateTask smsCalculateTask;
    private SmsVoiceSendTask smsVoiceSendTask;

    private Integer activeIndex = 0;
    private Integer selectedTemplateGroup;
    private List<Dictionary> smsTemplates;

    @Autowired
    private RegisterRecommendActivityService registerRecommendActivityService;
    @Autowired
    private SmsCalculateTaskService smsCalculateTaskService;
    @Autowired
    private SmsVoiceSendTaskService smsVoiceSendTaskService;
    @Autowired
    private SmsUserDefinitionService smsUserDefinitionService;
    @Autowired
    private PhoneLocationService phoneLocationService;

    private List<SmsUserDefinition> smsUserDefinitions;
    private List<ProductInfo> productInfos;
    private List<RegisterRecommendActivity> activityTypes;
    private List<ActivityChannelConfig> activityChannelConfigs;
    private Map<String, String> provinces;
    private Map<String, String> firstPartyNames;


    @PostConstruct
    private void init() {
        activityTypes = registerRecommendActivityService.findAll();
        smsUserDefinitions = smsUserDefinitionService.findAll();

        String calculateTaskId = getParamFromRequest("calculateTaskId");
        if (StringUtils.isNotBlank(calculateTaskId)) {
            smsCalculateTask = smsCalculateTaskService.findOne(NumberUtils.toLong(calculateTaskId));
            smsCondition = GsonUtils.toObject(smsCalculateTask.getCondition(), SmsCondition.class);
        }

        String sendVoiceTaskId = getParamFromRequest("sendVoiceTaskId");
        if (StringUtils.isNotBlank(sendVoiceTaskId)) {
            smsVoiceSendTask = smsVoiceSendTaskService.findOne(NumberUtils.toLong(sendVoiceTaskId));
            smsCalculateTask = smsCalculateTaskService.findOne(smsVoiceSendTask.getCalculateTaskId());
            smsCondition = GsonUtils.toObject(smsCalculateTask.getCondition(), SmsCondition.class);
            provinces = phoneLocationService.findDistinctProvince();
            firstPartyNames = smsVoiceSendTaskService.findDistinctFirstNames();
        }
    }


    public String onSend() {
        return redirect("/views/family/user-info/send-sms/send-voice-sms.xhtml?id=" + smsCalculateTask.getId());
    }

    public boolean canEditCalculateTask() {
        return smsCalculateTask != null && smsCalculateTask.getStatus() != SmsCalculateTaskStatus.CALCULATING
                && smsCalculateTask.getStatus() != SmsCalculateTaskStatus.CALCULATE_DONE;
    }

    public boolean canEditSendTask() {
        return smsVoiceSendTask != null && smsVoiceSendTask.getStatus() != SmsVoiceSendTaskStatus.SENDING
                && smsVoiceSendTask.getStatus() != SmsVoiceSendTaskStatus.SEND_DONE;
    }

    public void onResend() {
        smsVoiceSendTask = smsVoiceSendTaskService.resend(smsVoiceSendTask.getId());
        addSuccessfullyMessage();
    }

    public void onSaveCalculateTask() {
        SmsCalculateTask dbSmsCalculateTask = smsCalculateTaskService.findOne(smsCalculateTask.getId());

        if (dbSmsCalculateTask != null && dbSmsCalculateTask.getStatus() != SmsCalculateTaskStatus.CALCULATING
                && dbSmsCalculateTask.getStatus() != SmsCalculateTaskStatus.CALCULATE_DONE) {
            smsCalculateTaskService.saveOrUpdate(smsCalculateTask);
            addInfoMessage("保存计算任务成功");
        } else {
            addErrorMessage("计算任务保存失败, 状态已变更");
            smsCalculateTask = dbSmsCalculateTask;
        }
    }

    public void onSaveSendTask() {
        SmsVoiceSendTask dbSmsVoiceSendTask = smsVoiceSendTaskService.findOne(smsVoiceSendTask.getId());
        if (dbSmsVoiceSendTask != null && dbSmsVoiceSendTask.getStatus() != SmsVoiceSendTaskStatus.SENDING
                && dbSmsVoiceSendTask.getStatus() != SmsVoiceSendTaskStatus.SEND_DONE) {
            smsVoiceSendTaskService.saveOrUpdate(smsVoiceSendTask);
            addInfoMessage("保存发送任务成功");
        } else {
            addErrorMessage("发送任务保存失败, 状态已变更");
            smsVoiceSendTask = dbSmsVoiceSendTask;
        }
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(SMS_HIGH_QUALITY_USER_TYPE, SMS_ELIMINATE_BLACK_LIST, SMS_ELIMINATE_ACTION, SMS_ELIMINATE_ACTIVITY, SMS_USER_TYPE, PLATFORM,
                SMS_VOICE_PLATFORM_CHANNEL, SMS_EXCLUDE_SENDED_DAY, SMS_VOICE_ID);
    }

}
